from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session

class DBHelper(object):
    # # 单例模式
    def __new__(cls, *args, **kwargs):
        if not hasattr(DBHelper, "_instance"):
            DBHelper._instance = object.__new__(DBHelper)
        return DBHelper._instance

    def __init__(self, conf=None):

        if not conf:
            from Common.settings import DATABASE as conf
        uri = 'mysql+pymysql://%s:%s@%s:%s/%s?charset=utf8' % (conf['user'], conf['password'], conf['host'], conf['port'], conf['database'])
        self.engine = create_engine(uri, max_overflow=5, encoding='utf-8')
        session_factory = sessionmaker(bind=self.engine)
        self.Session = scoped_session(session_factory)
        #self.Session = session_factory()


    def close(self):
        pass
        # self.Session.close()

